<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="工单号" prop="docNo">
        <el-input
          v-model="queryParams.docNo"
          placeholder="请输入工单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="单据日期" prop="docDate">
        <el-date-picker
          v-model="queryParams.docDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="customerOrderNo">
        <el-input
          v-model="queryParams.customerOrderNo"
          placeholder="订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品品号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品品号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="itemSpecification">
        <el-input
          v-model="queryParams.itemSpecification"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="承诺交期" prop="planShipDate">
        <el-date-picker
          v-model="queryParams.planShipDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="执行状态" prop="exStatus">
        <el-select
          v-model="queryParams.exStatus"
          placeholder="请选择执行状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OPTICORE_EX_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          @click="openForm('create')"-->
        <!--          v-hasPermi="['opticore:work-order-cps:create']"-->
        <!--        >-->
        <!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
        <!--        </el-button>-->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['opticore:work-order-cps:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"   style="width: 100%">
      <el-table-column width="60" label="序号" type="index"  fixed="left"/>
      <el-table-column width="150" label="工单号" align="center" prop="docNo" />
      <el-table-column width="230" label="订单号" align="center" prop="customerOrderNo" />
      <el-table-column width="150" label="源订单号" align="center" prop="sourceSalesOrderDocNo" />
      <el-table-column width="150" label="销售订单号" align="center" prop="salesOrderDocNo" />
      <el-table-column width="200" label="单据日期" align="center" prop="docDate" />
      <el-table-column width="150" label="产线" align="center" prop="workCenterName" />
      <el-table-column width="150" label="产品品号" align="center" prop="itemCode" />
      <el-table-column width="200" label="承诺交期" align="center" prop="planShipDate" />
      <el-table-column width="150" label="产品规格" align="center" prop="itemSpecification" />
      <el-table-column width="120" label="BOM版次" align="center" prop="bomVersionTimes" />
      <el-table-column width="120" label="图号" align="center" prop="drawingNo" />
      <el-table-column width="120" label="预计产量" align="center" prop="planQty" />
      <el-table-column width="60" label="单位" align="center" prop="unitName" />
      <el-table-column width="120" label="执行状态" align="center" prop="exStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OPTICORE_EX_STATUS" :value="scope.row.exStatus" />
        </template>
      </el-table-column  >
      <el-table-column width="120" label="配纤状态" align="center" prop="otStates">
        <template #default="scope">
          {{ getotStatesval(scope.row.otStates) }}
        </template>
      </el-table-column  >
      <el-table-column width="150" label="操作" align="center" min-width="120px" fixed="right" >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.docNo)"
            v-hasPermi="['opticore:work-order-cps:update']"
          >
            详情
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.docNo)"
            v-hasPermi="['opticore:work-order-cps:update']"
          >
            排产
          </el-button>
        </template>
      </el-table-column  >
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WorkOrderCpsForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { WorkOrderCpsApi, WorkOrderCpsVO } from '@/api/opticore/workorder'
import WorkOrderCpsForm from './WorkOrderCpsForm.vue'
import { onMounted } from 'vue'

/** 工单管理 列表 */
defineOptions({ name: 'WorkOrderCps' })

onMounted(() => {
  console.log()
})

const getotStatesval = (otStates) => {
  if (otStates != null) {
    return getIntDictOptions(DICT_TYPE.OPTICORE_OT_STATUS).find((item) => {
      return item.value == otStates
    })?.label
  } else {
    return '--'
  }
}

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WorkOrderCpsVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  docNo: undefined,
  docDate: [],
  customerOrderNo: undefined,
  itemCode: undefined,
  itemSpecification: undefined,
  planShipDate: [],
  exStatus: undefined,
  moStatusList: ['未生产']
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WorkOrderCpsApi.getWorkOrderCpsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, docNo?: String) => {
  formRef.value.open(type, docNo)
}

/** 排产按钮操作 */
const handleDelete = async (docNo: string) => {
  try {
    // 排产的二次确认
    await message.confirm('确认排产工单' + docNo)
    // 发起排产
    await WorkOrderCpsApi.updateExStatus([docNo])
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WorkOrderCpsApi.exportWorkOrderCps(queryParams)
    download.excel(data, '工单管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
